<?PHP

/*
  ########################################################################################################################
  #
  #   KG Pager v2.0.1
  #   Class Name  : KG Pager Class
  #   Version     : 2.0.1
  #   Requirement : PHP4 >
  #   Build Date  : December 17, 2007 - Monday
  #   Developer   : Muharrem ER&#304;N (T�RK&#304;YE) - info@muharremerin.com - muharremerin.com - mhrrmrnr.com - kisiselgunce.com
  #   Licence     : GNU General Public License (c) 2007
  #
  ########################################################################################################################
 */

class Belka {

    public $total_records = NULL;
    public $start = NULL;
    public $scroll_page = NULL;
    public $per_page = NULL;
    public $total_pages = NULL;
    public $current_page = NULL;
    public $page_links = NULL;

    // total pages and essential variables
    function total_pages($pager_url, $total_records, $scroll_page, $per_page, $current_page) {
        $this->url = $pager_url;
        $this->total_records = $total_records;
        $this->scroll_page = $scroll_page;
        $this->per_page = $per_page;
        if (!is_numeric($current_page)) {
            $this->current_page = 1;
        } else {
            $this->current_page = $current_page;
        }
        if ($this->current_page == 1)
            $this->start = 0; else
            $this->start = ($this->current_page - 1) * $this->per_page;
        $this->total_pages = ceil($this->total_records / $this->per_page);
    }

    // page links
    function page_links($inactive_page_tag, $pager_url_last) {
        if ($this->total_pages <= $this->scroll_page) {
            if ($this->total_records <= $this->per_page) {
                $loop_start = 1;
                $loop_finish = $this->total_pages;
            } else {
                $loop_start = 1;
                $loop_finish = $this->total_pages;
            }
        } else {
            if ($this->current_page < intval($this->scroll_page / 2) + 1) {
                $loop_start = 1;
                $loop_finish = $this->scroll_page;
            } else {
                $loop_start = $this->current_page - intval($this->scroll_page / 2);
                $loop_finish = $this->current_page + intval($this->scroll_page / 2);
                if ($loop_finish > $this->total_pages)
                    $loop_finish = $this->total_pages;
            }
        }
        for ($i = $loop_start; $i <= $loop_finish; $i++) {
            if ($i == $this->current_page) {
                $this->page_links .= '<span ' . $inactive_page_tag . '>' . $i . '</span>';
            } else {
                $this->page_links .= '<span><a href="' . $this->url . $i . $pager_url_last . '.html">' . $i . '</a></span>';
            }
        }
    }

    // previous page
    function previous_page($previous_page_text, $pager_url_last) {
        if ($this->current_page > 1) {
            $this->previous_page = '<span><a href="' . $this->url . ($this->current_page - 1) . $pager_url_last . '.html">' . $previous_page_text . '</a></span>';
        }
    }

    // next page
    function next_page($next_page_text, $pager_url_last) {
        if ($this->current_page < $this->total_pages) {
            $this->next_page = '<span><a href="' . $this->url . ($this->current_page + 1) . $pager_url_last . '.html">' . $next_page_text . '</a></span>';
        }
    }

    // first page
    function first_page($first_page_text, $pager_url_last) {
        if ($this->current_page > 1) {
            $this->first_page = '<span><a href="' . $this->url . '1' . $pager_url_last . '.html">' . $first_page_text . '</a></span>'; // :)
        }
    }

    // last page
    function last_page($last_page_text, $pager_url_last) {
        if ($this->current_page < $this->total_pages) {
            $this->last_page = '<span><a href="' . $this->url . $this->total_pages . $pager_url_last . '.html">' . $last_page_text . '</a></span>';
        }
    }

    // pages functions set
    function pager_set($pager_url, $total_records, $scroll_page, $per_page, $current_page, $inactive_page_tag, $previous_page_text, $next_page_text, $first_page_text, $last_page_text, $pager_url_last) {
        $this->total_pages($pager_url, $total_records, $scroll_page, $per_page, $current_page);
        $this->page_links($inactive_page_tag, $pager_url_last);
        $this->previous_page($previous_page_text, $pager_url_last);
        $this->next_page($next_page_text, $pager_url_last);
        $this->first_page($first_page_text, $pager_url_last);
        $this->last_page($last_page_text, $pager_url_last);
    }

    /**
     *
     * @author Dingo
     * @param <int> $total_records
     * @param <int> $current_page
     * @param <int> $per_page
     * @param <int> $scroll_page
     * @param <array> $pager_url_last Podaj $this->param, a funkcja sama wytnie z argumentów kolejną stronę
     * @param <string> $pager_url
     * @param <array> $style
     */
    function pager_set2($total_records, $current_page, $per_page=10, $scroll_page=5,
            $param=false, $usun=false , $dodaj=false, $pager_url='strona,', $style=false) {
        $this->total_pages($pager_url, $total_records, $scroll_page, $per_page, $current_page);
        /* Ustawienie pager_url_last */
        if ($param !== false) {
            $pager_url_last = $this->ustawUrl($param, $usun, $dodaj);
        } else {
            $pager_url_last = '';
        }
        if ($style === false) {
            $this->page_links('id="current_page"', $pager_url_last);
            $this->previous_page('<', $pager_url_last);
            $this->next_page('>', $pager_url_last);
            $this->first_page('<<', $pager_url_last);
            $this->last_page('>>', $pager_url_last);
        } else {
            $this->page_links($style['inactive'], $pager_url_last);
            $this->previous_page($style['prev'], $pager_url_last);
            $this->next_page($style['next'], $pager_url_last);
            $this->first_page($style['first'], $pager_url_last);
            $this->last_page($style['last'], $pager_url_last);
        }
    }

    /**
     *
     * @param <array> $param parametry
     * @param <array> $usun parametry, które należy usunać z przekazanej tablicy $param
     * @param <array> $dodaj parametry, które należy dodać do przekazanej tablicy $param
     * $return <string> zwraca $pager_url_last
     */
    public function ustawUrl(&$param, $usun, $dodaj) {
        //usuwanie nieprzydatnych parametrów
        if (isset($param['strona'])) {
            unset($param['strona']);
        }
        if ($usun) {
            foreach ($usun as $v) {
                if (isset($param[$v])) {
                    unset($param[$v]);
                }
            }
        }
        //dowanie przydatnych parametrów
        if ($dodaj) {
            foreach ($dodaj as $k => $v) {
                $param[$k] = $v;
            }
        }
        //Tworzenie stringa z parametrów
        $tmp = ',';
        foreach ($param as $k => $v) {
            $tmp .= $k . ',' . $v;
        }
        return $tmp;
    }

}

?> 